package com.inossem.warehousestorage_inossem.adapter.refundStorage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.bean.common.GetOperatorGroupBean;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageAddOperatorUserGroupBean;

import java.util.List;

/**
 * 作者：wsy on 2017/11/22 14:36
 * <p>
 * 邮箱：445979770@qq.com
 */

public class RefundStoragePickingUserGroupListAdapter extends BaseAdapter {
    private Context mContext;
    private List<GetOperatorGroupBean.OperatorGroupBean> mGroupList;
    private LayoutInflater inflater;
    private GroupListAdapterListener mGroupListAdapterListener;

    public RefundStoragePickingUserGroupListAdapter(Context context, List<GetOperatorGroupBean.OperatorGroupBean> groupList, GroupListAdapterListener groupListAdapterListener) {
        mContext = context;
        mGroupList = groupList;
        mGroupListAdapterListener = groupListAdapterListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mGroupList.size();
    }

    @Override
    public Object getItem(int position) {
        return mGroupList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GetOperatorGroupBean.OperatorGroupBean groupListBean = mGroupList.get(position);
        RefundStoragePickingUserGroupListAdapter.ViewHolder viewHolder;
        if (convertView == null || (convertView != null && convertView.getTag() == null)) {
            convertView = inflater.inflate(R.layout.view_in_storage_select, null);
            viewHolder = new RefundStoragePickingUserGroupListAdapter.ViewHolder();
            //初始化控件
            initView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (RefundStoragePickingUserGroupListAdapter.ViewHolder) convertView.getTag();
        }
        //初始化数据
        initData(mContext, viewHolder, convertView, groupListBean);
        return convertView;
    }

    private void initView(RefundStoragePickingUserGroupListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.text = (TextView) view.findViewById(R.id.text);
        viewHolder.value = (TextView) view.findViewById(R.id.value);
    }

    private void initData(Context context, RefundStoragePickingUserGroupListAdapter.ViewHolder viewHolder, View view, GetOperatorGroupBean.OperatorGroupBean groupListBean) {
        viewHolder.text.setText(groupListBean.getGroup_id() + " - " + groupListBean.getGroup_name());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                mGroupListAdapterListener.onSelect(groupListBean.getGroup_name(), groupListBean.getGroup_id());
            }
        });
    }

    private class ViewHolder {
        private TextView text, value;
    }

    public static abstract class GroupListAdapterListener {
        public abstract void onSelect(String roleName, String roleCode);
    }
}
